﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Jzy.BLL.Mail;
using Jzy.Model.Mail;

using Jzy.Model;
using System.Collections.Generic;
using Jzy.Core.Common.Web;
using Frame.Lib.Common;
using Frame.Lib.Utilities;
using Frame.Lib.Core.Module;
using Frame.Lib.Enums;
using Frame.Lib.Core.Attach;
using Jzy.DAL;


public partial class Mail_Edit : PageEdit
{
    private MailBO _mail;
   
    private WriteMailMode mode;
    private string MailUserID
    {
        get
        {
            return Request["MailUserID"];
        }
    }
    private string _content = "";

    protected string Content
    {
        get
        {
            return _content.Replace("'", "\\'").Replace("\r\n", "<br/>");
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        mode = (WriteMailMode)int.Parse(Request["Mode"]);
        _mail = new MailBO(AppSession.UserID);
        if (!IsPostBack)
        {
            if (MailUserID != null)
            {
                DispInfo();
            }
        }
        AddAttr();
        this.AllowPageScroll();
    }

    private void DispInfo()
    {
        int mailID = _mail.GetMailID(MailUserID);
        MailInfo info = _mail.GetInfo(mailID);
        
        switch (mode)
        {
            case WriteMailMode.Reply://回复
                RecieveNames.Text = info.CreatorName.ToString();
                RecieveIDs.Value = "-" + info.CreatorID.ToString();
                Name.Text = "Re:" + info.Name;
                _content = "<div style='padding-left:10px;'><br/><br/><br/>========" + info.InputTime + "&nbsp;你在来信中写道========<br/>" +
                          "<div style='padding-left:10px;'><div style='border-left:solid 1px #a0a0a0;padding-left:10px;'>" + info.Content.ToString() + "</div></div></div>";
                break;
            case WriteMailMode.Forword://转发
                Name.Text = "Fw:" + info.Name;
                _content = "<div style='padding-left:10px;'><br/><br/><br/>========下面是转发邮件========<br/><br/>" +
                          "原邮件发件人：" + info.CreatorName + "<br/>" +
                          "原邮件收件人：" + info.RecieveNames + "<br/>" +
                          "原邮件发送时间：" + info.InputTime + "<br/><br/>" +
                          "<div style='padding-left:10px;'><div style='border-left:solid 1px #a0a0a0;padding-left:10px;'>" + info.Content.ToString() + "</div></div>" +
                          "===============================</div>";


                AttachBO attach = new AttachBO(OTableName.MailAttach);
                IEnumerable<AttachInfo> list = attach.GetList(mailID);
                rptAttach.DataSource = list;
                rptAttach.DataBind();
                break;
            case WriteMailMode.Draft://草稿
                EntityUtil.SetControl(pnlMain, info);
                _content = info.Content;
                break;
        }
        if (mode == WriteMailMode.Forword || mode == WriteMailMode.Draft)
        {
            AttachBO attach = new AttachBO(OTableName.MailAttach);
            IEnumerable<AttachInfo> list = attach.GetList(mailID);
            rptAttach.DataSource = list;
            rptAttach.DataBind();
        }
    }

    private void AddAttr()
    {
        this.Form.Attributes.Add("enctype", "multipart/form-data");
        JSUtil.ClickBtn(lnkSend, btnSend);
        JSUtil.ClickBtn(lnkSave, btnSave);

        JSUtil.ShowDeptUserTreeDlg(RecieveIDs, RecieveNames, false, null);
        JSUtil.ShowDeptUserTreeDlg(CopyToIDs, CopyToNames, false,null);
    }

    protected void btnSend_Click(object sender, EventArgs e)
    {
        MailInfo info = GetInfo();

        bool flag = _mail.Send(info, Request.Files, mode);

        if (flag == true && mode == WriteMailMode.Forword)
        {
            List<int> lstAttachIDs = new List<int>();
            foreach (RepeaterItem item in rptAttach.Items)
            {
                if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
                {
                    HiddenField hidID = (HiddenField)item.FindControl("hidID");
                    CheckBox chkSelect = (CheckBox)item.FindControl("chkSelect");
                    if (chkSelect.Checked)
                    {
                        lstAttachIDs.Add(int.Parse(hidID.Value));
                    }
                }
            }
            _mail.CopyAttach(lstAttachIDs, (int)info.ID);//复制附件
        }

        if (flag)
        {
            ModuleBO module = new ModuleBO();
            int moduleID = (int)module.GetIDByCode(ModuleCode.ReceiveBox);
            JSUtil.SelectModule(moduleID, "发送成功");

            //JSUtil.ResponseJS("alert('发送成功');window.location.href = 'Recieve.aspx';");
            //JSUtil.Alert("发送成功");
        }
        else
        {
            this._content = Request.Form["content"];
            JSUtil.Alert("发送失败");
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        MailInfo info = GetInfo();
        bool flag = _mail.SaveToDraft(info, Request.Files,MailUserID);
        if (flag)
        {
            ModuleBO module = new ModuleBO();
            int moduleID = (int)module.GetIDByCode(ModuleCode.DraftBox);
            JSUtil.SelectModule(moduleID, "保存草稿成功");

            //JSUtil.ResponseJS("alert('保存草稿成功');window.location.href = 'Draft.aspx';");

        }
        else
        {
            JSUtil.Alert("保存草稿失败");
        }
    }

    private MailInfo GetInfo()
    {
        MailInfo info = new MailInfo();
        EntityUtil.SetEntity(pnlMain, info);
        
       

        //只有来源于草稿箱的，保存时才更新，其余全为增加，如来源于回复或转发也要保存ＩＤ以便于改变状态。
        if (mode == WriteMailMode.Draft || mode == WriteMailMode.Forword || mode == WriteMailMode.Reply)
        {          
            info.ID = _mail.GetMailID(MailUserID);
        }
        info.CreatorID = AppSession.UserID;
        info.CreatorName = AppSession.UserName;
        info.InputTime = DateTime.Now;
        info.Content = Request.Form["content"];

        if (info.Name == null || info.Name == "")
        {
            info.Name = "无标题[来自" + info.CreatorName + "]";
        }

        return info;
    }
  
}
